import {createStore} from 'vuex'
import * as constConfig from '../constData/ConstData'
import axios from '../utils/axios'
import {ElLoading} from 'element-plus'
import {EncryptPkcs7} from "../utils/crypto";

export default createStore({
    state: {
        options: {
            lock: true,
            text: '内容加载中......',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)',
        },
    },
    getters: {},
    mutations: {
        SET_AUTH_TOKEN: (state, authorizationToken) => {
            localStorage.setItem(constConfig.tokenHeader, authorizationToken)
        },
        SET_REFRESH_AUTH_TOKEN: (state, refreshAuthorization) => {
            localStorage.setItem(constConfig.refreshTokenHeader, refreshAuthorization)
        },
        REMOVE_AUTH_TOKEN: () => {
            localStorage.setItem(constConfig.tokenHeader, "")
            localStorage.setItem(constConfig.refreshTokenHeader, "")
        },
        showLoading(state, LoadingSwitch) {
            let loadingInstance = ElLoading.service(state.options)
            LoadingSwitch ? ElLoading.service(state.options) : loadingInstance.close()
        }
    },
    actions: {
        LOGIN: (context, form) => {
            return new Promise((resolve, reject) => {
                form.password = undefined ? form.password : EncryptPkcs7(form.password)
                axios.post(constConfig.prefixApi + '/auth/login',
                    form,
                    {headers: {'Content-Type': constConfig.contentTypeForm}}).then(res => {
                    //如果后端发送用户信息过来，可以在这里进行存储，此处暂未做任何处理

                    resolve()
                }).catch(error => reject(error))
            })
        },
    },
    modules: {}
})
